package base.day10;

import com.sun.source.tree.NewClassTree;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class  WindowDraw extends JFrame {
    //用数组定义图片编号
    private int[][] photoarray = {
            {1, 2, 3, 4},
            {4, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    private int[][] winarr={
            {1, 2, 3, 4},
            {4, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    private int x0;
    private int y0;
    JButton shangBotton;
    private JButton xiaBotton;
    private JButton zuoBotton;
    private JButton youBotton;
    private JButton qiuZhuBotton;
    private JButton chongZhiBotton;
    private JPanel imagesjp;

    public WindowDraw() {
        photoJfream();
        randomPhoto();
        compOnent();
        addButtonEvent();
        this.setVisible(true);
    }

    //窗口基本设置
    public void photoJfream() {
        this.setTitle("动漫拼图");
        this.setSize(960, 565);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }

    //求助
    public void success(){
        photoarray= new int[][]{
                {1, 2, 3, 4},
                {4, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}
        };
        shangBotton.setEnabled(false);
        xiaBotton.setEnabled(false);
        zuoBotton.setEnabled(false);
        youBotton.setEnabled(false);
    }
    //判断是否成功
    public boolean iswin(){
        for (int i = 0; i < winarr.length; i++) {
            for (int j = 0; j < winarr[i].length; j++) {
                if (winarr[i][j]!=photoarray[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
    //移动的图形重新绘制
    public void Repaint(){
        //移除面板上所有组件
        imagesjp.removeAll();
        //遍历数组得到编号
        for (int i = 0; i < photoarray.length; i++) {
            for (int j = 0; j < photoarray[i].length; j++) {
                JLabel imagelable = new JLabel(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\" + photoarray[i][j] + ".png"));
                imagelable.setBounds(j * 90, i * 90, 90, 90);
                imagesjp.add(imagelable);
            }
        }
        this.add(imagesjp);
        //重新绘制窗体
        imagesjp.repaint();
    }
    //给按钮添加事件
    public void addButtonEvent(){
        shangBotton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("上");
                if (x0==3){
                    return;
                }
                //位置交换
                photoarray[x0][y0]=photoarray[x0+1][y0];
                photoarray[x0+1][y0]=0;
                x0=x0+1;
                //判断是否拼接成功
                if (iswin()){
                    success();
                }
                //调用重绘方法
                Repaint();

            }
        });
        xiaBotton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("下");
                if (x0==0) {
                    return;
                }
                photoarray[x0][y0]=photoarray[x0-1][y0];
                photoarray[x0-1][y0]=0;
                x0=x0-1;
                //判断是否拼接成功
                if (iswin()){
                    success();
                }
                Repaint();
            }
        });
        zuoBotton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("左");
                if (y0==3){
                    return;
                }
                photoarray[x0][y0]=photoarray[x0][y0+1];
                photoarray[x0][y0+1]=0;
                y0=y0+1;
                //判断是否拼接成功
                if (iswin()){
                    success();
                }
                Repaint();
            }
        });
        youBotton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("右");
                if (y0==0){
                    return;
                }
                photoarray[x0][y0]=photoarray[x0][y0-1];
                photoarray[x0][y0-1]=0;
                y0=y0-1;
                //判断是否拼接成功
                if (iswin()){
                    success();
                }
                Repaint();
            }
        });
        chongZhiBotton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("重置");
                randomPhoto();
                Repaint();
                shangBotton.setEnabled(true);
                xiaBotton.setEnabled(true);
                zuoBotton.setEnabled(true);
                youBotton.setEnabled(true);
            }
        });
        qiuZhuBotton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("求助");
                success();
                Repaint();
            }
        });
    }

    //图片打乱
    public void randomPhoto() {
        Random rd = new Random();
        for (int i = 0; i < photoarray.length; i++) {
            for (int j = 0; j < photoarray[i].length; j++) {
                int x = rd.nextInt(photoarray.length);
                int y = rd.nextInt(photoarray[i].length);
                int temp = photoarray[i][j];
                photoarray[i][j] = photoarray[x][y];
                photoarray[x][y] = temp;
            }
        }
        //找到0图片的位置
        wc:for (int i = 0; i < photoarray.length; i++) {
            for (int j = 0; j < photoarray[i].length; j++) {
                if (photoarray[i][j]==0){
                    x0=i;
                    y0=j;
                    break wc;
                }
            }

        }System.out.println(x0+","+y0);

    }

    //窗口组件的设置
    public void compOnent() {
        JLabel titlelable = new JLabel(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\title.png"));
        titlelable.setBounds(354, 27, 232, 57);
        this.add(titlelable);

        //创建面板
        imagesjp = new JPanel();
        imagesjp.setBounds(150, 114, 360, 360);
        imagesjp.setLayout(null);
        //遍历数组得到编号
        for (int i = 0; i < photoarray.length; i++) {
            for (int j = 0; j < photoarray[i].length; j++) {
                JLabel imagelable = new JLabel(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\" + photoarray[i][j] + ".png"));
                imagelable.setBounds(j * 90, i * 90, 90, 90);
                imagesjp.add(imagelable);
            }
        }
        this.add(imagesjp);
        JLabel canZhaoTulable = new JLabel(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\canzhaotu.png"));
        canZhaoTulable.setBounds(574, 114, 122, 121);
        this.add(canZhaoTulable);

        //上下左右，求助，重置按钮
        //上
         shangBotton = new JButton(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\shang.png"));
        shangBotton.setBounds(732, 265, 57, 57);
        this.add(shangBotton);
        //左
         zuoBotton = new JButton(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\zuo.png"));
        zuoBotton.setBounds(650, 347, 57, 57);
        this.add(zuoBotton);
        //下
         xiaBotton = new JButton(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\xia.png"));
        xiaBotton.setBounds(732, 347, 57, 57);
        this.add(xiaBotton);
        //右
         youBotton = new JButton(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\you.png"));
        youBotton.setBounds(813, 347, 57, 57);
        this.add(youBotton);
        //重置
         chongZhiBotton = new JButton(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\chongzhi.png"));
        chongZhiBotton.setBounds(786, 444, 108, 45);
        this.add(chongZhiBotton);
        //求助
         qiuZhuBotton = new JButton(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\qiuzhu.png"));
        qiuZhuBotton.setBounds(626, 444, 108, 45);
        this.add(qiuZhuBotton);

        //背景图
        JLabel backGround = new JLabel(new ImageIcon("E:\\java\\java project\\java-code\\src\\base\\day10\\images\\background.png"));
        backGround.setBounds(960, 530, 0, 0);
        this.add(backGround);

    }
}
